<template>
  <el-select
    @change="sendName"
    placeholder="请输入关键字搜索"
    :clearable="true"
    v-model="companyName"
    filterable
  >
    <!-- :filter-method="dataFilter" -->
    <el-option
      v-for="(item,index) in allCompanyFilterData"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    ></el-option>
  </el-select>
</template>

<script>
import Http from "../libs/http.js";
export default {
  name: "CompanySelect",
  data() {
    return {
      companyName: "",
      allCompany: [],
      selected: [],
      allCompanyFilterData: []
    };
  },
  props: ["p_value", ":p_name", "disab"],
  created() {
    this.getall();
  },
  watch:{
    p_value(v){
      this.companyName=v;
    }
  },
  methods: {
    dataFilter(val) {
      if (val) {
        this.allCompanyFilterData = this.allCompany.filter(item => {
          if (
            pinyinUtil.getFirstLetter(item.name).indexOf(val.toUpperCase()) !=
            -1
          ) {
            return true;
          }
        });
      } else {
        this.allCompanyFilterData = this.allCompany;
      }
    },
    sendName(id) {

      let item = this.getItemById(id);
      if (item) {
        this.$emit("update:p_value", item.id);
        this.$emit("update:p_name", item.name);
        this.$emit("sendName", item.id);
      } else {
        this.$emit("update:p_value", "");
        this.$emit("update:p_name", "");
        this.$emit("sendName", "");
      }
    },
    getall() {
      Http.api.get("company/allPermission").then(res => {
        this.allCompany = res.data;
        this.allCompanyFilterData = this.allCompany;
      });
    },
    getItemById(id){
      for( let index in this.allCompany){
        if(this.allCompany[index].id == id){
          return this.allCompany[index] ;
        }
      }
    }
  }
};
</script>

